<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/customer/custMessage.class.php');

class ajax_message {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询会员消息列表
                    $this->searchMessageList();
                    break;

                case 2://查询会员消息
                    $this->searchMessage();
                    break;

                case 3://显示会员信息
                    $this->searchCustomerMsg();
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('edit','del'))) {
                        returnJsonData('参数错误');
                    }
                    if($_REQUEST['act'] == 'edit'){

                        $this->saveAddMessage();//新增 修改 会员消息

                    }else if($_REQUEST['act'] == 'del'){

                        $this->deleteMessageMaster();//删除会员消息

                    }

                    break;

                case 101:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del'))) {
                        returnJsonData('参数错误');
                    }
                    if($_REQUEST['act'] == 'del'){
                        $this->deleteMessage();//删除会员消息
                    }

                    break;



                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 会员消息列表
     */
    private function searchMessageList(){

        global $G_PGLOB;

        //实例化会员消息类
        $custMessgae = new custMessage();

        $arg_array = $_REQUEST;
        $message_array = $custMessgae->searchCustMessageMaster( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($message_array) {

            $edit_permission = $G_PGLOB->has_permission(3010);

            foreach($message_array as &$data) {

                if ($edit_permission) {

                    $operation_html = '';
                    $operation_html .= '<a href="javascript:void(0);" onclick="message.edit.open(' . $data['message_id'] . ');">修改</a>&nbsp;&nbsp;&nbsp;';
                    $operation_html .= '<a href="javascript:void(0);" onclick="message.import(' . $data['message_id'] . ')">导入</a>&nbsp;&nbsp;&nbsp;';
                    $operation_html .= '<a href="javascript:void(0);" onclick="message.sentMessage(' . $data['message_id'] . ');">已发消息</a>';

                    $result_array['rows'][] = array (
                        'id' => $data['message_id'],
                        'cell' => array(
                            $operation_html,
                            $data['createTime'],
                            $data['message_title']
                        )
                    );

                } else {

                    $result_array['rows'][] = array (
                        'id' => $data['message_id'],
                        'cell' => array(
                            $data['createTime'],
                            $data['message_title']

                        )
                    );

                }

            }

        }


        echo json_encode($result_array);
    }

    /**
     * 查询会员信息方式
     */
    private function searchMessage() {

        if (!isset($_REQUEST['message_id']) || !quanli_is_int($_REQUEST['message_id'])) {
            returnJsonData('参数错误');
        }

        //实例会员消息类
        $custMessage = new custMessage();

        //查询会员消息方式
        $arg_array = array(

            'message_id' => $_REQUEST['message_id']
        );
        $message_master = $custMessage->searchCustMessageMaster( $arg_array );

        if ($message_master) {
            returnJsonData('ok', 200, $message_master[0]);
        } else {
            returnJsonData('会员消息信息没有找到', 404);
        }

    }

    /**
     * 新增/修改地址
     */
    private function saveAddMessage() {

        global $G_SHOP;

        //实例会员消息类
        $custMessage = new custMessage();

        //新增/修改
        $arg_array = $_REQUEST;
        $arg_array['creator'] = $G_SHOP->currentUserID;
        $arg_array['message_title'] = $arg_array['save_message_title'];
        $result_array = $custMessage->editMessageMaster( $arg_array );
        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 删除会员消息
     */
    private function deleteMessageMaster () {

        //实例会员消息类
        $custMessage = new custMessage();

        //批量删除会员消息
        $result_array = $custMessage->deleteMessageMaster( $_REQUEST['p_array'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 查询会员发送消息
     */
    private function searchCustomerMsg() {

        //实例会员消息类
        $custMessage = new custMessage();

        $message_array = $custMessage->searchCustMessage( $_REQUEST );

        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($message_array) {

            foreach($message_array as &$message) {

                $result_array['rows'][] = array(
                    'id' => $message['uuid'],
                    'cell' => array(
                        $message['custID'],
                        $message['createTime'],
                        $message['is_read'] ? '是' : '否',
                        $message['is_reply'] ? '是' : '否',
                        $message['reply_time'],
                        $message['reply_content'],
                    )
                );

            }
        }

        echo json_encode($result_array);

    }

    /**
     * 删除会员发送消息
     */
    private function deleteMessage () {

        //实例会员消息类
        $custMessage = new custMessage();

        //批量删除会员发送消息
        $result_array = $custMessage->deleteMessage( $_REQUEST['p_array'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }


}